package servlets.local;

import hibernate.method.TableEntityMethods;
import hibernate.model.TableEntity;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.google.gson.Gson;

/**
 * Servlet implementation class deleteTable
 */
@WebServlet("/deleteTable")
public class deleteTable extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public deleteTable() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String local_id = request.getParameter("local_id");
		response.setCharacterEncoding("UTF-8");
		List<TableEntity> list = new ArrayList<TableEntity>();
		List<String> list2 = new ArrayList<String>();
		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		TableEntityMethods cm = new TableEntityMethods();
		list = cm.listTablesByLocalId(sessionFactory, session,
				Integer.parseInt(local_id));

		for (TableEntity te1 : list) {
			list2.add(String.valueOf(te1.getTable_id()));
		}

		String json = new Gson().toJson(list2);
		response.setContentType("application/json");
		response.getWriter().write(json);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		// String local = (String) request.getParameter("local");
		String table = (String) request.getParameter("table");

		PrintWriter out = response.getWriter();
		response.setContentType("text/html");

		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session s = sessionFactory.openSession();
		s.beginTransaction();

		TableEntityMethods tem = new TableEntityMethods();
		TableEntity t = tem.getTableById(sessionFactory, s,
				Integer.parseInt(table));
		s.delete(t);

		try {
			s.getTransaction().commit();
			out.println("<script type=\"text/javascript\">alert(\"Stolik zostal usuniety\"); window.location.href = \"management/index.jsp?page=events&group=deleteTable\";</script>");
		} catch (Exception e) {
			out.println("<script type=\"text/javascript\">alert(\"Stolik nie zostal usuniety\"); window.location.href = \"management/index.jsp?page=events&group=deleteTable\";</script>");
		}

	}

}
